package com.anbinh.doctor.fragment.benhnhan;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.anbinh.doctor.R;
import com.anbinh.doctor.adapter.PatientImagesAdapter;
import com.anbinh.doctor.model.DataResponse;
import com.anbinh.doctor.model.ImageData;
import com.anbinh.doctor.model.ResponseImage;
import com.anbinh.doctor.model.ResponsePatient.Patient;
import com.anbinh.doctor.model.ResponseUploadImg;
import com.anbinh.doctor.model.UserLogin;
import com.anbinh.doctor.request.controller.ServerController;
import com.anbinh.doctor.service.request.OnRequest;
import com.anbinh.doctor.utils.Utils;
import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar;

public class FragmentImagesPatients extends Fragment {
	private static FragmentImagesPatients fragImages = null;
	private View mView;
	private CircleProgressBar loadding;
	private ListView mListView;
	private PatientImagesAdapter adapter;
	private List<ImageData> mImagesList =null;
	private UserLogin user;
	private int mPosition;
	private UserLogin userLogin;
	private Patient currentPatient;

	public static FragmentImagesPatients newInstance() {
		if (fragImages == null)
			fragImages = new FragmentImagesPatients();
		return fragImages;
	}

	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		mListView = (ListView) mView.findViewById(R.id.listView1);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				mPosition = position;
				Bundle bundle = new Bundle();
				bundle.putInt(Utils.KEY, Utils.VIEW_IMG_PATIENT);
				bundle.putString(Utils.TITLE,
						getResources().getString(R.string.xem_anh));
				Utils.getNewIntent((Activity) getActivity(), bundle);
			}

		});
		mImagesList = new ArrayList<ImageData>();
		currentPatient = FragmentListPatients.newInstance().getCurrentPatient();
		loadding = (CircleProgressBar) mView.findViewById(R.id.loadding);
		adapter = new PatientImagesAdapter(getActivity(),
				R.layout.item_image_patient, mImagesList);
		mListView.setAdapter(adapter);
		onLoadListImg();
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		userLogin = Utils.getAccountModel(getActivity(), Utils.KEY_USER);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_message, null);
		return mView;
	}

	public void onLoadListImg() {

		if (userLogin == null)
			return;
		loadding.setVisibility(View.VISIBLE);

		ServerController.getInstance(getActivity()).reuquestListImagesPatients(
				userLogin.getData().getToken(), currentPatient.getId(),
				FragmentListPatients.newInstance().getCurrentDoctorId(),
				new OnRequest<ResponseImage>() {

					@Override
					public void onSuccess(ResponseImage data) {
						loadding.setVisibility(View.INVISIBLE);
						if (getActivity() == null)
							return;
						if (data.getResponsecode() >= 0) {
							mImagesList.addAll(data.getData().getListItem());
							adapter.notifyDataSetChanged();
						}
					}

					@Override
					public void onError(int errorCode, String message) {
						loadding.setVisibility(View.INVISIBLE);
						
					}
				});
	}

	protected void onDelete(final ImageData item) {
		UserLogin userLogin = Utils.getAccountModel(getActivity(),
				Utils.KEY_USER);
		if (userLogin == null)
			return;
		ServerController.getInstance(getActivity()).requestdoRemoveImage(
				userLogin.getData().getToken(), item.getId(),
				new OnRequest<DataResponse>() {

					@Override
					public void onSuccess(DataResponse data) {
						if (getActivity() == null)
							return;

						Utils.showMessage(((Activity) getActivity()),
								data.getDescription());
					}

					@Override
					public void onError(int errorCode, String message) {

					}

				});
	}

	public int getmPosition() {
		return mPosition;
	}

	public void updateInfo(Intent data) {
		if (getActivity() == null)
			return;
		user = Utils.getAccountModel(getActivity(), Utils.KEY_USER);

		String token = user.getData().getToken();
		Uri selectedImage = data.getData();
		File file = new File(Utils.getRealPathFromURI(getActivity(),
				selectedImage));
		loadding.setVisibility(View.VISIBLE);
		Utils.disableEnableControls(false, mView);
		ServerController.getInstance(getActivity()).requestUploadImgPatients(
				token, currentPatient.getId(), file, "patient_img",
				FragmentListPatients.newInstance().getCurrentDoctorId(),
				new OnRequest<ResponseUploadImg>() {

					@Override
					public void onSuccess(ResponseUploadImg data) {
						if (getActivity() == null)
							return;

						loadding.setVisibility(View.GONE);
						Utils.disableEnableControls(true, mView);
						if (data.getResponsecode() >= 0) {
							ImageData item = new ImageData();
							item.setThumb(data.getData().getImage());
							item.setImage(data.getData().getImage());
							item.setId(data.getData().getId());
							item.setUploader(user.getData().getUserInfo()
									.getFull_name());
							item.setCreated_at(Utils
									.getCurrentDayName("HH:mm:ss dd-MM-yyyy"));
							mImagesList.add(item);
							adapter.notifyDataSetChanged();
						}
						Utils.showMessage(getActivity(), data.getDescription());
					}

					@Override
					public void onError(int errorCode, String message) {
						loadding.setVisibility(View.GONE);
						Utils.disableEnableControls(true, mView);
						Utils.showMessage(getActivity(), message);
					}
				});
	}

	public List<ImageData> getImagesList() {
		return mImagesList;
	}

	public void addNewImg(String thumb, int id) {

	}
}
